//
//  SubjectListViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/11/6.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "SubjectListViewController.h"
#import "SubjectListTableViewCell.h"
#import "SubjiectListModel.h"
#import "NewSubjectModel.h"
#import "SinglePageSubViewController.h"
#import "MorePageSubViewController.h"
@interface SubjectListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)TB_TableView *homeTableView;

@property(nonatomic,strong)SubjiectListModel *subjectData;

@end

@implementation SubjectListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"检查主题";
    
    [self.view addSubview:self.homeTableView];
    
    [self loadDatas];
    
    // Do any additional setup after loading the view.
}




-(TB_TableView*)homeTableView
{
    if (!_homeTableView) {
        
        _homeTableView=[[TB_TableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        
        _homeTableView.delegate=self;
        
        _homeTableView.dataSource=self;
        
        _homeTableView.estimatedRowHeight=70;
        
        _homeTableView.tableFooterView=[[UIView alloc]init];
        
        [_homeTableView registerNib:[UINib nibWithNibName:@"SubjectListTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        
        
    }
    
    return _homeTableView;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.subjectData.data.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
    
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SubjectData *data=self.subjectData.data[indexPath.section];
    
    SubjectListTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.data=data;
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewAutomaticDimension;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 20;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SubjectData *data=self.subjectData.data[indexPath.section];
    
    [self addNewSubject:data];
}


-(void)tableView:(UITableView *)tableView willDisplayFooterView:(nonnull UIView *)view forSection:(NSInteger)section
{
     view.tintColor=TBColorTableViewBg;
    
}


#pragma mark-新增检查记录

-(void)addNewSubject:(SubjectData*)data
{
       [MBProgressHUD showActivityMessageInView:@"正在新增检查记录..." timer:10.0];

    BaseReqApi *Api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/inspectAdd.json" andrequestTime:5 andParams:@{@"subjectId":[NSString stringWithFormat:@"%ld",data.subjectId],@"objId":self.objId,@"checkUserId":[NSString stringWithFormat:@"%ld",[LoginStatuesModel getStaffId]]} andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [Api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        
        [self stopLoadingAnimation];
        
        NewSubjectModel *model=[NewSubjectModel mj_objectWithKeyValues:responseObject];
        
        if (responseStatus==1) {
            //请求成功
            [MBProgressHUD showSuccessMessage:@"新增成功"];
            
            if (model.data.viewType==2) {
                
                //单页
                SinglePageSubViewController *singlePage=[SinglePageSubViewController new];
                
                singlePage.SubModel=model.data;
                
                singlePage.title=self.Tiptitle;
                
                singlePage.ContentUrl=@"/index.php/FoodCheckApi/inspectSinglePageItemList.json";
                
                singlePage.CheckLookSelfRecord=YES;
                
                [self.navigationController pushViewController:singlePage animated:YES];
                
            }else{
                
                //多页
                
                MorePageSubViewController *morePage=[MorePageSubViewController new];
                
                morePage.SubModel=model.data;
                
                morePage.title=self.Tiptitle;
                
                morePage.CheckLookSelfRecord=YES;
            
                [self.navigationController pushViewController:morePage animated:YES];
            }
            
        
        }else if (responseStatus==0||responseStatus==2)
        {
            [MBProgressHUD showErrorMessage:message];
     
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
            
        }
        
        
    }];

}


#pragma mark-检查记录
-(void)loadDatas
{
    [self showLoadingAnimation];
    
    BaseReqApi *Api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/inspectSubjectList.json" andrequestTime:5 andParams:self.reqModel.mj_keyValues andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [Api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        
        [self stopLoadingAnimation];
        
        self.subjectData=[SubjiectListModel mj_objectWithKeyValues:responseObject];
        
        if (responseStatus==1) {
            //请求成功
            if (self.subjectData.data.count==0) {
                
                self.Type=TBNODateType;
                
            }else{
                
                [self.homeTableView reloadData];
                
            }
        }else if (responseStatus==0||responseStatus==2)
        {
            [MBProgressHUD showErrorMessage:message];
            
            self.Type=TBReqFalie;
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
            
        }
        
        
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
